<?php

require_once '../config.php';

session_start();

require_once '../function.php';

//添加用户
function add_users()
{
    global $message;
    if (empty($_POST['email'])) {
        $message = '请输入邮箱';
        return;
    }
    if (empty($_POST['slug'])) {
        $message = '请输入姓名';
        return;
    }
    if (empty($_POST['nickname'])) {
        $message = '请输入昵称';
        return;
    }
    if (empty($_POST['password'])) {
        $message = '请输入密码';
        return;
    }

    //获取用户输入的数据
    $email = $_POST['email'];
    $slug = $_POST['slug'];
    $nickname = $_POST['nickname'];
    $password = $_POST['password'];

    //连接数据库

    $rows = xiu_IDU("insert into users values (null,'{$slug}','{$email}','{$password}','{$nickname}','/static/uploads/avatar.jpg',null,'activated');");


    $message = $rows <= 0 ? '添加失败' : '添加成功';

}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    //add_users();
}


//删除用户

function delete_users()
{
    global $message;
    $id = $_GET['id'];
    $affect_rows = xiu_IDU('delete from users where id in (' . $id . ');');
    $message = $affect_rows <= 0 ? '删除失败' : '删除成功';
}

//delete_users();


//编辑用户
function edit_users()
{
    global $message;
    global $edit_id;
    if (empty($_POST['email'])) {
        $message = '请输入邮箱';
        return;
    }
    if (empty($_POST['slug'])) {
        $message = '请输入姓名';
        return;
    }
    if (empty($_POST['nickname'])) {
        $message = '请输入昵称';
        return;
    }
    if (empty($_POST['password'])) {
        $message = '请输入密码';
        return;
    }


    //需要提交编辑的数据
    $edit_email = empty($_POST['email']) ? $edit_id['email'] : $_POST['email'];
    $edit_slug = empty($_POST['slug']) ? $edit_id['slug'] : $_POST['slug'];
    $edit_nickname = empty($_POST['nickname']) ? $edit_id['nickname'] : $_POST['nickname'];
    $edit_password = empty($_POST['password']) ? $edit_id['password'] : $_POST['password'];

    //呈现的数据
    $edit_id['email'] = $edit_email;
    $edit_id['slug'] = $edit_slug;
    $edit_id['nickname'] = $edit_nickname;
    $edit_id['password'] = $edit_password;


    var_dump($edit_email,$edit_slug,$edit_nickname,$edit_password);

    $affect_rows = xiu_IDU("update users set slug ={$edit_slug},email={$edit_email},`password` ={$edit_password},nickname ={$edit_nickname},avatar = '/static/uploads/avatar.jpg',bio = '',`status` = 'activated' where id = {$edit_id};");


    var_dump($affect_rows);
    $message = $affect_rows <=0?'编辑失败':'编辑成功';

}

if (isset($_GET['id'])) {

    //拿到用户id对应的数据
    $edit_id = xiu_query_one("select * from users where id = {$_GET['id']};");

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        edit_users();
    }

}

//查询数据
$users = xiu_query_all('select * from users');

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>Users &laquo; Admin</title>
    <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="/static/assets/vendors/font-awesome/css/font-awesome.css">
    <link rel="stylesheet" href="/static/assets/vendors/nprogress/nprogress.css">
    <link rel="stylesheet" href="/static/assets/css/admin.css">
    <script src="/static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
<script>NProgress.start()</script>

<div class="main">
    <?php include 'inc/navbar.php'; ?>

    <div class="container-fluid">
        <div class="page-title">
            <h1>用户 <a href="<?php echo $_SERVER['PHP_SELF'];?>">返回 < </a></h1>
        </div>
        <!-- 有错误信息时展示
        <?php if (isset($message)): ?>
         <div class="alert <?php echo $message === '添加成功' ? ' alert-success' : ' alert-danger' ?>;">
          <?php echo $message; ?>
        </div>
        <?php endif; ?>
        -->
        <div class="row">
            <div class="col-md-4">

                <?php if (isset($edit_id)): ?>
                    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
                        <h2>编辑用户</h2>
                        <div class="form-group">
                            <label for="email">邮箱</label>
                            <input id="email" class="form-control" name="email" type="email" placeholder="邮箱"
                                   value="<?php echo $edit_id['email'] ?>">
                        </div>
                        <div class="form-group">
                            <label for="slug">别名</label>
                            <input id="slug" class="form-control" name="slug" type="text" placeholder="slug"
                                   value="<?php echo $edit_id['slug']; ?>">
                            <p class="help-block">https://zce.me/author/<strong>slug</strong></p>
                        </div>
                        <div class="form-group">
                            <label for="nickname">昵称</label>
                            <input id="nickname" class="form-control" name="nickname" type="text" placeholder="昵称"
                                   value="<?php echo $edit_id['nickname']; ?>">
                        </div>
                        <div class="form-group">
                            <button class="btn btn-primary" type="submit">保存</button>
                        </div>
                    </form>
                <?php else: ?>
                    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
                        <h2>添加新用户</h2>
                        <div class="form-group">
                            <label for="email">邮箱</label>
                            <input id="email" class="form-control" name="email" type="email" placeholder="邮箱">
                        </div>
                        <div class="form-group">
                            <label for="slug">别名</label>
                            <input id="slug" class="form-control" name="slug" type="text" placeholder="slug">
                            <p class="help-block">https://zce.me/author/<strong>slug</strong></p>
                        </div>
                        <div class="form-group">
                            <label for="nickname">昵称</label>
                            <input id="nickname" class="form-control" name="nickname" type="text" placeholder="昵称">
                        </div>
                        <div class="form-group">
                            <label for="password">密码</label>
                            <input id="password" class="form-control" name="password" type="password" placeholder="密码">
                        </div>
                        <div class="form-group">
                            <button class="btn btn-primary" type="submit">添加</button>
                        </div>
                    </form>

                <?php endif; ?>

            </div>
            <div class="col-md-8">
                <div class="page-action">
                    <!-- show when multiple checked -->
                    <a class="btn btn-danger btn-sm" href="<?php $_SERVER['PHP_SELF']; ?>" id="btn_delete"
                       style="visibility: hidden;">批量删除</a>
                </div>
                <table class="table table-striped table-bordered table-hover">
                    <thead>
                    <tr>
                        <th class="text-center" width="40"><input type="checkbox" data-id="-1"></th>
                        <th class="text-center" width="80">头像</th>
                        <th>邮箱</th>
                        <th>别名</th>
                        <th>昵称</th>
                        <th>状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                    </thead>
                    <tbody>

                    <?php foreach ($users as $item): ?>
                        <tr>
                            <td class="text-center"><input type="checkbox" data-id="<?php echo $item['id']; ?>"></td>
                            <td class="text-center"><img class="avatar" src="/static/assets/img/default.png"></td>
                            <td><?php echo $item['email']; ?></td>
                            <td><?php echo $item['slug']; ?></td>
                            <td><?php echo $item['nickname']; ?></td>
                            <td>激活</td>
                            <td class="text-center">
                                <a href="<?php $_SERVER['PHP_SELF']; ?>?id=<?php echo $item['id']; ?>"
                                   class="btn btn-default btn-xs">编辑</a>
                                <a href="<?php $_SERVER['PHP_SELF']; ?>?id=<?php echo $item['id']; ?>"
                                   class="btn btn-danger btn-xs">删除</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>


                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php $current_page = 'users'; ?>
<?php include 'inc/sidebar.php'; ?>

<script src="/static/assets/vendors/jquery/jquery.js"></script>
<script src="/static/assets/vendors/bootstrap/js/bootstrap.js"></script>

<script>

    $(function () {
        let btn_remove = $('#btn_delete');
        let inputAll = $('thead input');
        let input = $('tbody input');
        let tableInput = $('table input');
        let visibility = null;
        let arr = [];
        let id = null;


        tableInput.on('change', function () {
            //利用h5自定义属性data-保存每个元素的id,存放到数组,便于删除数据
            id = $(this).data('id');
            if ($(this).prop('checked')) {
                arr.push(id);
            } else {
                arr.splice(arr.indexOf(id), 1);
            }

            if ($('tbody input:checked').length === 0) {
                visibility = 'hidden';
            } else {
                visibility = 'visible';
            }

            btn_remove.css('visibility', visibility);
            //search: 搜索资源路径
            btn_remove.prop('search', '?id=' + arr);

        });

        inputAll.on('change', function () {
            input.prop('checked', $(this).prop('checked'));
            if ($('thead input:checked').length === 0) {
                visibility = 'hidden';
                arr.length = 0;
            } else {
                visibility = 'visible';
            }
            btn_remove.css('visibility', visibility);
        });


        input.on('change', function () {
            let checkedLen = $('tbody input:checked').length;
            let inputLen = input.length;
            inputAll.prop('checked', checkedLen === inputLen);
        });

    });


</script>
<script>NProgress.done()</script>
</body>
</html>
